package com.saint.farmshop.Service.ServiceImpl;

import com.saint.farmshop.Dto.ArticleDto;
import com.saint.farmshop.Entity.Article;
import com.saint.farmshop.Entity.ProductionDetail;
import com.saint.farmshop.Entity.User;
import com.saint.farmshop.Mapper.MapperImpl.ArticleMapperImpl;
import com.saint.farmshop.Mapper.MapperImpl.ProductionMapperImpl;
import com.saint.farmshop.Mapper.MapperImpl.UserMapperImpl;
import com.saint.farmshop.Mapper.UserMapper;
import com.saint.farmshop.Service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 文章服务接口实现类
 *
 * @author Saint
 * @date 2022/06/15
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapperImpl articleMapper;

    @Resource
    UserMapperImpl userMapper;

    @Resource
    ProductionMapperImpl productionMapper;

    @Override
    public Article addArticle(Article article) {
        return articleMapper.addArticle(article);
    }

    @Override
    public Article updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    @Override
    public boolean deleteArticle(String id) {
        return articleMapper.deleteArticle(id);
    }

    @Override
    public ArticleDto getArticleById(String id) {
        Article article = articleMapper.getArticleById(id);
        ArticleDto articleDto = getArticleDto(article);
        return articleDto;
    }

    @Override
    public List<ArticleDto> getArticleList(Integer pageNum, Integer pageSize) {
        List<ArticleDto> articleDtoList = new ArrayList<>();
        List<Article> articleList = articleMapper.getArticleList(pageNum, pageSize);
        for (Article article : articleList) {
            ArticleDto temp = getArticleDto(article);
            articleDtoList.add(temp);
        }
        return articleDtoList;
    }

    @Override
    public List<Article> getArticleList() {
        return articleMapper.getArticleList();
    }

    ArticleDto getArticleDto(Article article){
        User userTemp = userMapper.selectUserByOpenId(article.getOpenId());
        ProductionDetail productionDetailTemp = productionMapper.getProductionDetailById(article.getProductionId());
        ArticleDto temp = new ArticleDto(article,userTemp,productionDetailTemp);
        return temp;
    }
}
